package cn.bandeqing.user.controller;

import cn.bandeqing.base.domain.CurrentAdminUser;
import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.base.resp.Table;
import cn.bandeqing.stp.StpAdminUtil;
import cn.bandeqing.user.req.*;
import cn.bandeqing.user.resp.*;
import cn.bandeqing.user.rpc.IUserRpc;
import cn.bandeqing.worker.req.WithdrawReq;
import io.github.linpeilie.Converter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/user")
@RestController
@Tag(name = "用户")
public class UserController {

    @DubboReference
    private IUserRpc userRpc;
    @Resource
    private Converter converter;

    @Operation(summary = "changePwd")
    @PostMapping("/changePwd")
    public Resp<Void> changePwd(@RequestBody @Valid ChangePwdReq req) {
        return userRpc.changePwd(req);
    }

    @Operation(summary = "withdraw")
    @PostMapping("/withdraw")
    public Resp<Void> withdraw(@RequestBody @Valid UserWithdrawReq req) {
        return userRpc.withdraw(req);
    }

    @Operation(summary = "balance")
    @PostMapping("/balance")
    public Resp<UserBalanceResp> balance() {
        return userRpc.balance();
    }

    @Operation(summary = "userUpdateBasic")
    @PostMapping("/userUpdateBasic")
    public Resp<Void> userUpdateBasic(@RequestBody @Valid UserUpdateBasicReq req) {
        if (StrUtil.isBlank(req.getUsAddress()) && StrUtil.isBlank(req.getUsPhone()) && StrUtil.isBlank(req.getUsEmail()) && StrUtil.isBlank(req.getUsDesc())) {
            return Resp.error("请输入修改信息");
        }
        return userRpc.userUpdateBasic(req);
    }

    @Operation(summary = "userBasic")
    @PostMapping("/userBasic")
    public Resp<UserBasicResp> userBasic() {
        return userRpc.userBasic();
    }

    @Operation(summary = "userRoles")
    @PostMapping("/userRoles")
    public Resp<List<Long>> userRoles(@RequestBody @Valid UserRolesReq req) {
        return userRpc.userRoles(req);
    }

    @Operation(summary = "userRole")
    @PostMapping("/userRole")
    public Resp<Void> userRole(@RequestBody @Valid UserRoleReq req) {
        return userRpc.userRole(req);
    }

    @Operation(summary = "roles")
    @PostMapping("/roles")
    public Resp<List<RolesResp>> roles() {
        return userRpc.roles();
    }

    @Operation(summary = "表格")
    @PostMapping("/table")
    public Resp<Table<UserTable>> table(@RequestBody @Valid UserTableReq req) {
        return userRpc.table(req);
    }

    @Operation(summary = "创建或更新")
    @PostMapping("/saveOrUpdate")
    public Resp<Void> saveOrUpdate(@RequestBody @Valid UserSaveOrUpdateReq req) {
        userRpc.saveOrUpdate(req);
        return Resp.success();
    }

    @Operation(summary = "登录")
    @PostMapping("/login")
    public Resp<Void> login(@RequestBody @Valid LoginReq loginReq) {
        LoginResp resp = userRpc.login(loginReq);
        CurrentAdminUser currentAdminUser = converter.convert(resp, CurrentAdminUser.class);
        StpAdminUtil.login(currentAdminUser);
        return Resp.success();
    }

    @Operation(summary = "当前用户")
    @PostMapping("/current")
    public Resp<CurrentResp> current() {
        CurrentAdminUser current = StpAdminUtil.getCurrent();
        return Resp.success(new CurrentResp(current.getUsNickName(),
                "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif?imageView2/1/w/80/h/80",
                current.getUsId(), current.getUsId() == 1 ? "admin" : "", current.getRoles(), current.getAuths(), current.getSuperAdmin()));
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Resp<Void> logout() {
        StpAdminUtil.logout();
        return Resp.success();
    }

}
